<template>
  <Dialoger v-model="isShow" :onConfirm="handleSubmit" :dialogCls="'d-paner-md'" title="编辑">
    <template v-slot:body>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">环境名称</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.code" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">编码</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.key" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">父级</label>
          </div>
          <div class="d-form-el">
            <FormSelecter v-model="state.pid" :data="treeData" value-key="id" :text-key="'code'" slide-max-height="200px" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">描述</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.description" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">全局环境</label>
          </div>
          <div class="d-form-el">
            <Switcher v-model="state.volume" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">排序</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.order" class="d-form-input" type="text" placeholder="数值越大越靠前" />
          </div>
        </div>
        <FormCalendar
          v-if="isEdit"
          v-model="state.updateTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="修改时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-12"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">修改人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.updateUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
        <FormCalendar
          v-if="isEdit"
          v-model="state.createTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="创建时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-12"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">创建人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.createUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
      </div>
    </template>
  </Dialoger>
</template>

<script>
import { PanerForm } from '@daelui/vdog/dist/pages.js'
import { Dialoger, FormCalendar, FormSelecter, Switcher } from '@daelui/vdog/dist/components'
import { zh as locale } from '@daelui/dogjs/dist/locale'
import { parser, objecter } from '@daelui/dogjs/dist/components'
import dictionarySer from '../../../service/action/dictionary.action'
import naveBus from '../business/nave'

export default {
  components: {
    PanerForm,
    FormCalendar,
    Dialoger,
    FormSelecter,
    Switcher
  },

  data () {
    return {
      isShow: false, // 窗口是否显示
      isEdit: false, // 是否是编辑模式,
      // 树形环境数据
      treeData: [],
      // 编辑信息
      editInfo: {},
      // 表单数据
      state: {
        id: '',
        cat: '',
        code: '',
        key: '',
        value: '',
        order: 1,
        description: '',
        pid: ''
      }
    }
  },

  methods: {
    /**
     * @function 显示与隐藏
    */
    show (data) {
      this.isShow = !!data
      data = data || {}
      this.editInfo = data
      if (data.edit) {
        this.isEdit = true
        this.state = objecter.clone(data.item)
      } else {
        this.isEdit = false
        let newItem = {
          id: '',
          cat: 'nave-envirment',
          code: '',
          key: '',
          value: '',
          order: 1,
          description: '',
          pid: ''
        }
        // 添加子级
        if (data.parent) {
          newItem.pid = data.item.id || ''
        }
        this.state = newItem
      }
      // 查询导航数据
      naveBus.queryTreeEnv().then((list) => {
        list = list.filter((item) => {
          return item.id !== this.state.id
        })
        this.treeData = list
      })
    },

    /**
     * @function 提交
    */
    handleSubmit (b) {
      let defer = null
      let data = Object.assign({}, this.state)
      data.layout = parser.stringify(data.layout)
      // 编辑
      if (this.isEdit) {
        defer = dictionarySer.editItem(data)
      } else { // 添加
        defer = dictionarySer.addItem(data)
      }
      return defer.then(result => {
        let b = true
        if (result.success) {
          Dialoger.success(locale.action.success)
          this.$emit('onSubmitSuccess', data)
        } else {
          b = false
          Dialoger.danger(locale.action.fail)
        }
        return b
      })
    }
  }
}
</script>

<style lang="less" scoped>
.d-row {
  .d-form-group:last-child {
    margin-bottom: 0;
  }
}
.d-form-label {
  width: 90px;
}
.d-form-textarea {
  min-height: 320px;
}
</style>
